﻿/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://github.com/shenyczz/CSharpKit
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/

using System;
using System.Runtime.Serialization;

namespace CSharpKit.Numerics.LinearAlgebra
{
    [Serializable]
    [DataContract(Namespace = "urn:CSharpKit/Numerics/LinearAlgebra")]
    public class MatrixStorageSparse<T> : MatrixStorage<T>
        where T : struct, IEquatable<T>, IFormattable
    {
        public MatrixStorageSparse(int row, int column)
            : base(row, column) { }

        /// <summary>
        /// The array containing the row indices of the existing rows. Element "i" of the array gives the index of the
        /// element in the <see cref="Values"/> array that is first non-zero element in a row "i".
        /// The last value is equal to ValueCount, so that the number of non-zero entries in row "i" is always
        /// given by RowPointers[i+i] - RowPointers[i]. This array thus has length RowCount+1.
        /// </summary>
        [DataMember(Order = 1)]
        public readonly int[] RowPointers;

        /// <summary>
        /// An array containing the column indices of the non-zero values. Element "j" of the array
        /// is the number of the column in matrix that contains the j-th value in the <see cref="Values"/> array.
        /// </summary>
        [DataMember(Order = 2)]
        public int[] ColumnIndices;

        /// <summary>
        /// Array that contains the non-zero elements of matrix. Values of the non-zero elements of matrix are mapped into the values
        /// array using the row-major storage mapping described in a compressed sparse row (CSR) format.
        /// </summary>
        [DataMember(Order = 3)]
        public T[] Values;



        // public static SparseCompressedRowMatrixStorage<T> OfArray(T[,] array)
        // {
        //     var storage = new SparseCompressedRowMatrixStorage<T>(array.GetLength(0), array.GetLength(1));
        //     var rowPointers = storage.RowPointers;
        //     var columnIndices = new List<int>();
        //     var values = new List<T>();

        //     for (int row = 0; row < storage.RowCount; row++)
        //     {
        //         rowPointers[row] = values.Count;
        //         for (int col = 0; col < storage.ColumnCount; col++)
        //         {
        //             if (!Zero.Equals(array[row, col]))
        //             {
        //                 values.Add(array[row, col]);
        //                 columnIndices.Add(col);
        //             }
        //         }
        //     }

        //     rowPointers[storage.RowCount] = values.Count;
        //     storage.ColumnIndices = columnIndices.ToArray();
        //     storage.Values = values.ToArray();
        //     return storage;
        // }

        //}}@@@
    }


}
